<template>
  <div class="contentCate">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-select
          v-model="searchParams.status"
          placeholder="请选择状态"
          size="mini"
        >
          <el-option
            v-for="item in statusArr"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('signPanels/index')"
          >搜索</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="add"
          v-if="has('signPanels/store')"
          >添加</el-button
        >
      </el-row>
      <el-table
        :data="data.data"
        v-loading="table_loading"
        row-key="id"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="ID"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="day"
          label="第几天"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="num"
          label="数量"
          align="center"
        ></el-table-column>

        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="changeStatus(scope.row)"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="activeValue"
              :inactive-value="inactiveValue"
              v-if="has('signPanels/destroy')"
            >
            </el-switch>

            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="activeValue"
              :inactive-value="inactiveValue"
              disabled
              v-else
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row.id)"
              v-if="has('signPanels/show')"
            ></el-button>
            <!-- <el-button
                    type="danger"
                    size="mini"
                    icon="el-icon-delete"
                    @click="del(scope.row.id, scope.$index)"
                    v-if="has('signPanels/destroy')"
                  ></el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>

    <el-card class="box-card" v-if="status === 'add'">
      <div slot="header">
        <el-page-header @back="back" content="添加"></el-page-header>
      </div>
      <el-form
        :model="editParams"
        :rules="rules"
        ref="form"
        label-width="120px"
      >
        <el-form-item label="类型" prop="type">
          <el-select v-model="editParams.type">
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editParams.status">
            <el-option
              v-for="item in statusArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券" v-if="editParams.type == 2">
          <el-select v-model="editParams.ext_id">
            <el-option
              v-for="item in couponData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="反悔卡" v-if="editParams.type == 3">
          <el-select v-model="editParams.ext_id">
            <el-option
              v-for="item in backCardData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数量" prop="num">
          <el-input v-model="editParams.num" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="submit('form', 1)"
            v-if="has('signPanels/store')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" v-if="status === 'edit'">
      <div slot="header">
        <el-page-header @back="back" content="编辑"></el-page-header>
      </div>
      <el-form
        :model="editParams"
        :rules="rules"
        ref="form"
        label-width="120px"
      >
        <el-form-item label="类型" prop="type">
          <el-select v-model="editParams.type">
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editParams.status">
            <el-option
              v-for="item in statusArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券" v-if="editParams.type == 2">
          <el-select v-model="editParams.ext_id">
            <el-option
              v-for="item in couponData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="反悔卡" v-if="editParams.type == 3">
          <el-select v-model="editParams.ext_id">
            <el-option
              v-for="item in backCardData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数量" prop="num">
          <el-input v-model="editParams.num" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="submit('form', 2)"
            v-if="has('signPanels/update')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";
export default {
  name: "contentCate",
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中

      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        // 搜索参数
        page: 1,
        limit: 10,
        type: ""
      },
      editParams: {
        type: "",
        num: "",
        status: "",
        ext_id: ""
      },
      activeValue: 1,
      inactiveValue: 0,
      rules: {
        num: [{ required: true, message: "请输入数量", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
        type: [{ required: true, message: "请选择类型", trigger: "blur" }]
      },
      statusArr: [
        { id: 1, title: "显示" },
        { id: 2, title: "隐藏" }
      ],
      typeArr: [
        { id: 1, title: "钻石" },
        { id: 2, title: "优惠券" },
        { id: 3, title: "反悔卡" }
      ],
      couponData: [],
      backCardData: []
    };
  },
  created() {
    this.getData();
    this.getCoupon();
    this.getBackCard();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.signPanels
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    changeStatus(e) {
      this.load();
      api.signPanels
        .del(e.id)
        .then(res => {
          this.unload();
          if (res.data.code === 200) {
            this.$message.success("保存成功");
            this.init();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 获取所有数据
    getCoupon() {
      api.common
        .getCoupon()
        .then(res => {
          if (res.data.code == 200) {
            this.couponData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getBackCard() {
      api.common
        .getBackCard()
        .then(res => {
          if (res.data.code == 200) {
            this.backCardData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    back() {
      this.status = "index";
    },
    // 初始化
    init() {
      this.editParams = {
        name: "",
        remark: "",
        expired_day: "",
        status: "",
        blind_box_id: ""
      };
      this.status = "index";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    // 添加页面
    add() {
      this.status = "add";
    },
    // 编辑页面
    edit(id) {
      this.load();
      api.signPanels
        .show(id)
        .then(res => {
          this.unload();
          if (res.data.code == 200) {
            this.status = "edit";
            this.editParams = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 保存添加或编辑
    submit(formName, type) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          this.load();

          // 添加
          if (type === 1) {
            api.signPanels
              .store(this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.reset();
                }
              })
              .catch(error => {
                console.log(error);
              });
          } else if (type == 2) {
            // 编辑
            api.signPanels
              .edit(this.editParams.id, this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.reset();
                }
              })
              .catch(error => {
                console.log(error);
              });
          }
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    },
    // 删除
    del(id, index) {
      this.$confirm("此操作将删除快递, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          api.signPanels
            .del(id)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.data.data.splice(index, 1);
                this.data.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="less">
.contentCate {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
